package com.gz.university.crm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gz.university.crm.dao.ISysUserDao;
import com.gz.university.crm.entity.SysUser;
import com.gz.university.crm.entity.SysUserGroup;
import com.gz.university.crm.form.SysUserForm;
import com.gz.university.crm.service.ISysUserService;

@Transactional(readOnly = true)
@Service(value = ISysUserService.SERVICE_NAME)
public class SysUserServiceImpl implements ISysUserService {
	private ISysUserDao sysUserDao;

	public ISysUserDao getSysUserDao() {
		return sysUserDao;
	}

	@Resource(name = ISysUserDao.DAO_NAME)
	public void setSysUserDao(ISysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}

	/*
	 * @Override public boolean isExistSysUserByNameAndPassword(String name,
	 * String password) { // TODO Auto-generated method stub boolean flag =
	 * false; if (StringUtils.isNotBlank(name) &&
	 * StringUtils.isNotBlank(password)) { String pwd =
	 * this.sysUserDao.findPassword(name); if (password.equals(pwd)) { flag =
	 * true; } } return flag; }
	 */

	@Override
	public SysUser findSysUserByNameAndPassword(String name, String password) {
		// TODO Auto-generated method stub
		SysUser sysUser = null;
		String hql = "from SysUser s where s.name is :name and s.password is :password";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("name", name);
		params.put("password", password);
		if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(password)) {
			List<SysUser> list = this.sysUserDao.find(hql, params);
			if (list != null && list.size() > 0) {
				sysUser = list.get(0);
			}
		}

		return sysUser;
	}
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void saveSysUser(SysUser sysUser) {
		// TODO Auto-generated method stub
		this.sysUserDao.save(sysUser);
	}

	@Override
	public List<SysUser> findSysUserByConditions(SysUserForm sysUserForm) {
		// TODO Auto-generated method stub
		String hql = "from SysUser t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(sysUserForm, hql, params);
		List<SysUser> list = (List<SysUser>) this.sysUserDao
				.find(hql, params);
		return list;
	}

	private String addWhere(SysUserForm sysUserForm, String hql,
			Map<String, Object> params) {
		// TODO Auto-generated method stub
		if (StringUtils.isNotBlank(sysUserForm.getName())) {
			hql+=" and t.name like :name";
			params.put("name", "%%"+sysUserForm.getName()+"%%");
		}
		if (StringUtils.isNotBlank(sysUserForm.getName())) {
			hql+=" and t.cnname like :cname";
			params.put("cnname", "%%"+sysUserForm.getCnname()+"%%");
		}
		if (StringUtils.isNotBlank(sysUserForm.getStatus())) {
			hql+=" and t.status is :status";
			params.put("status", sysUserForm.getStatus());
		}
		if (StringUtils.isNotBlank(sysUserForm.getGroupId())) {
			hql+=" and t.sysUserGroup.sysUserGroupId is :sysUserGroupId";
			params.put("sysUserGroupId", Integer.parseInt(sysUserForm.getGroupId()));
		}
		return hql;
	}
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void deleteSysUsersByIds(Integer[] idsIntegers) {
		// TODO Auto-generated method stub
		this.sysUserDao.deleteSysUsers(idsIntegers);
	}
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void updateSysUserStatus(Integer[] ids) {
		// TODO Auto-generated method stub
    	String hql="update SysUser s set s.status = 'Y' where s.sysUserId in :ids";
		this.sysUserDao.updateStatusByIds(hql,ids);
	}
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void updateStatusDisable(Integer[] ids) {
		// TODO Auto-generated method stub
    	String hql="update SysUser s set s.status = 'N' where s.sysUserId in :ids";
		this.sysUserDao.updateStatusByIds(hql, ids);
	}

	@Override
	public SysUser findSysUserById(Integer id) {
		// TODO Auto-generated method stub
		return this.sysUserDao.get(SysUser.class, id);
	}
    @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	@Override
	public void updateSysUser(SysUser sysUser) {
		// TODO Auto-generated method stub
		this.sysUserDao.update(sysUser);
	}

}
